'use strict';

require.config({
	baseUrl: 'js',
	//urlArgs: "v=" + (new Date()).getTime(),
	paths: {
		jquery: 'vendor/jquery/dist/jquery.min',
		director: 'vendor/director/build/director.min',
		highcharts: [
			'vendor/highcharts/highcharts',
			'vendor/highcharts/highcharts-more',
			'vendor/highcharts/modules/exporting'
		],
		react: 'vendor/react/react.min',
		reactAddons: 'vendor/react/react-with-addons.min',
		JSXTransformer: 'vendor/react/JSXTransformer',
		cookieStorage: 'vendor/cookie-storage/dist/cookie-storage',
		json2: 'vendor/json/json2',
		xdate: 'vendor/xdate/src/xdate',
		moment: [
			'vendor/moment/min/moment.min',
			'vendor/moment/min/locales.min',
			'vendor/moment/min/moment-with-locales.min'
		],
		jPages: 'vendor/jPages/js/jPages',
		numberFormat: 'vendor/number-format.js/lib/format.min',
		bootstrap:[
			'vendor/bootstrap/dist/js/bootstrap.min'
		],
		datetimepicker:[
			'vendor/bootstrap-datetimepicker/js/bootstrap-datetimepicker',
			'vendor/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN'
		],
		datetimepickerLanguage:'vendor/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN',
		fancybox:'vendor/fancybox/source/jquery.fancybox.pack',
		async:'vendor/async/lib/async'
	},
	shim: {//设置依赖
		'highcharts': {
			'deps': ['jquery'],
			'exports': 'Highcharts'      // Still gotta make this available //使用全局的Highcharts做为模块名称
		},
		'datetimepickerLanguage':{//因datetimepickerLanguage依赖datetimepicker,所以次代码是优先加载datetimepicker
			'deps':['datetimepicker']
		},
		'datetimepicker':{
			'deps':['css!vendor/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min']
		},
		'bootstrap':{
			'deps':['jquery']
		},
		'fancybox':{
			'deps':[
				'jquery',
				'css!vendor/fancybox/source/jquery.fancybox.css'
			]
		},
		'async':{
			'deps':[
				'vendor/async/deps/nodeunit',
				'css!vendor/async/deps/nodeunit.css'
			]
		}
	},
	map: {
		'*': {
	    	'css': 'vendor/require-css/css'
	    }
	}
});

require([
	'jquery',
	'cookieStorage',
	'json2',
	'director',
	'components/login',
	'components/logout',
	'components/userInfo',
	'components/dataShow',
	'components/oauthManage',
	'components/taskManage',
	'components/sqlManage'
	], function(
		$, 
		CookieStorage, 
		Json, 
		director, 
		login, 
		logout, 
		userInfo, 
		showData, 
		oauthManage, 
		taskManage,
		sqlManage) {

	//导航切换
	$(function(){
		
		var href = window.location.href;
		var href_arr = href.split('#');
		var activeTabVal = href_arr[1];
		var navObj = $('ul.dl-nav');
		if(activeTabVal){
			navObj.find('li').each(function(){
				var obj = $(this);
				var href = obj.find('a').attr('href');
				if(href == '#'+activeTabVal){
					obj.addClass('active');
				}else{
					obj.removeClass('active');
				}
			});
		}

		navObj.find('li').click(function(){
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
		})
	});
		
	function checkLogin() {

		window.cookie_Storage = new CookieStorage.CookieStorage();
		var qeeniao_login = cookie_Storage.getItem('qeeniao_login');
		if(qeeniao_login){
			qeeniao_login = JSON.parse(qeeniao_login);
		}else{
			qeeniao_login = {};
		}

		if(qeeniao_login.islogin){
			login.doLogin();
			window.location.href = '?#/showData';
			return true;
		}else{
			login.render();
		}

		login.render();
		return false;
	}

	checkLogin();

	var route = {
		'/logout':logout.do,
		'/login':login.render,
		'/userinfo': [checkLogin, userInfo.render],
		'/showData': [checkLogin, showData.render],
		'/oauthManage': [checkLogin, oauthManage.render],
		'/taskManage': [checkLogin, taskManage.render],
		'/sqlManage': [checkLogin, sqlManage.render]
	};
	var router = Router(route);
	router.init();
});